package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.network.entity.AtUserEntity;
import com.zw.yzk.kabu.utils.CircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/7/3.
 */

public class AtUserAdapter extends RecyclerView.Adapter<AtUserAdapter.Holder> {

    private Context context;
    private List<AtUserEntity.User> dataList;

    public AtUserAdapter(List<AtUserEntity.User> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<AtUserEntity.User> dataList) {
        this.dataList.clear();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    public void add(List<AtUserEntity.User> list) {
        int start = dataList.size();
        if (list != null) {
            this.dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_at_user_result, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final AtUserEntity.User model = dataList.get(position);
        holder.bind(context, model);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.onItemClicked(model);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.name)
        TextView name;


        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Context context, AtUserEntity.User user) {
            Glide.with(context)
                    .load(user.headImg)
                    .transform(new CircleTransform(context))
                    .error(R.drawable.ic_default_head)
                    .into(image);
            name.setText(user.nickName);

        }
    }

    public interface OnItemClickedListener {
        void onItemClicked(AtUserEntity.User user);
    }
    public OnItemClickedListener listener;
    public void setOnItemClickedListener(OnItemClickedListener listener) {
        this.listener = listener;
    }
}
